﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.DAL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class ES_ActivityRequest : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        IEmployeeBLL EmpBLL = new EmployeeBLL();
        ConvertNullable cn = new ConvertNullable();
        ActivityBLL actBLL = new ActivityBLL();
        //AC_ActivitysBLL ActivityBLL = new AC_ActivitysBLL();
        ActivityRequestBLL ActReqBLL = new ActivityRequestBLL();
        
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDataGrid();
                LoadActivity();
                LoadUpper();
            }
        }

        private void LoadDataGrid()
        {

            DataSet ListDataCommand = ActReqBLL.GetReqStatus(ssMng.EmpSeq);
            DataTable table = ListDataCommand.Tables[0];
            RadGrid2.DataSource = ListDataCommand.Tables[0];
            //RadGrid2.DataSource = "";
            RadGrid2.DataBind();
        }
        protected void RadGrid2_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {

            }
            if (e.CommandName == "AddData")
            {
                UpdateUI2("AddNew");
            }
            LoadDataGrid();
        }
        protected void LoadUpper()
        {
            DataSet ListSalaryCommand = EmpBLL.LoadEmployees(null, "1", true, "");
            DataTable table = ListSalaryCommand.Tables[0];
            cbUpper.DataSource = ListSalaryCommand.Tables[0];
            cbUpper.DataBind();
        }

        protected void LoadActivity()
        {
            //DataSet ListDataCommand = ActivityBLL.GetDataActivity("","1","1");
            //DataTable table = ListDataCommand.Tables[0];
            //cbActivity.DataSource = ListDataCommand.Tables[0];
            //cbActivity.DataBind();
            
        }
        protected void cbActivity_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadDetail();
        }
        protected void LoadDetail()
        {
            int Sequence = Int32.Parse(cbActivity.SelectedValue);
            DataSet ListDataCommand = actBLL.LoadActivity(Sequence, (int?)null, (DateTime?)null, (DateTime?)null);
            if (ListDataCommand.Tables.Count > 0)
            {
                DataTable table = ListDataCommand.Tables[0];
                if (table.Rows.Count > 0)
                {
                    lbActDescr.Text = table.Rows[0]["act_descr"].ToString();
                    lbSchedule.Text = table.Rows[0]["job_site_name"].ToString();
                    lbDateSignStard.Text = string.Format("{0:dd/MM/yyyy}", table.Rows[0]["date_sign_start"]);
                    lbDateSignEnd.Text = string.Format("{0:dd/MM/yyyy}", table.Rows[0]["date_sign_end"]);
                    lbDateActStard.Text = string.Format("{0:dd/MM/yyyy}", table.Rows[0]["date_activity_start"]);
                    lbDateActEnd.Text = string.Format("{0:dd/MM/yyyy}", table.Rows[0]["date_activity_end"]);
                    lbActLimit.Text = table.Rows[0]["act_limit"].ToString();
                    lbAccepTrainee.Text = table.Rows[0]["Total"].ToString();
                    lbSchedule.Text = table.Rows[0]["location_text"].ToString();
                    lbTotalTrainee.Text = table.Rows[0]["Candidate"].ToString();
                    lbJobsite.Text = table.Rows[0]["job_site_name"].ToString();
                    lbcity.Text = table.Rows[0]["city_name"].ToString();
                    lbCountry.Text = table.Rows[0]["country_name"].ToString();                   

                }
            }
        }
        protected void btnSave2_Click(object sender, EventArgs e)
        {
            string strRefType = "ขออนุมัติการสมัครเพื่อเข้าร่วมกิจกรรม"+ cbActivity.Text;
            ActReqBLL.InsertMsgSend(cbActivity.SelectedValue, "hrm_ac_activitys", strRefType, ssMng.EmpSeq, cbUpper.SelectedValue, "", "", ssMng.EmpSeq);              
            LoadDataGrid();
            UpdateUI2("Save");
        }
        protected void btnCancel2_Click(object sender, EventArgs e)
        {
            UpdateUI2("Cancel");
        }virtual 
        protected void UpdateUI2(string strCommand)
        {
            switch (strCommand)
            {
                case "Save":
                    PanelGrid.Visible = true;
                    PanelAddData2.Visible = false;

                    break;

                case "AddNew":
                    PanelGrid.Visible = false;
                    PanelAddData2.Visible = true;
                    btnSave2.Visible = true;

                    break;

                case "Cancel":
                    PanelGrid.Visible = true;
                    PanelAddData2.Visible = false;

                    break;

                case "Update":
                    PanelGrid.Visible = false;
                    PanelAddData2.Visible = true;
                    btnSave2.Visible = false;

                    break;
            }
        }
    }
}
